/*
 * SamplePostData.java
 *
 * Created on April 16, 2007, 2:36 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Comm.Net;

import Activity.ActivityChooserScreen;
import Comm.Util.Base64;
import Comm.Util.Logger;
import Experience.ExpSample;
import Experience.ExpUI;
import java.util.Random;
import java.util.Vector;

/**
 *
 * @author dmgreen
 */
public class SamplePostData extends PostData {
    
    /**
     * Creates a new instance of SamplePostData
     */
    public SamplePostData(ExpSample exp_sample) {
        super(ExpUI.UPLOADER_URL, exp_sample.getUID());
    
        addGlucose(exp_sample.getGlucose());
        processExperienceSample(exp_sample);
        
        this.setMsgType(BaseData.MSGTYPE_SAMPLE);
        
    }
    
    public void processExperienceSample(ExpSample es) {
      
        /* This function pulls all of the data out of the ExperienceSample object
         * and puts it into this PostData object, where it is in a form suitable
         * for POSTing to the upploader script. Several different pieces of information
         * must be processed:
         */
        
        // Process all the photos that were taken
        Vector imageVec = es.getImages();
        for (int i=0; i < imageVec.size(); i++) {
            //addLogMessage("adding an Image to PostData");
            byte[] the_image_array = (byte[]) imageVec.elementAt(i);
            //addLogMessage("Length of this array is " + the_image_array.length);
            addImage(the_image_array);
            //addLogMessage("FINISHED adding an Image to PostData");
        }
        
        // Process all the audio clips that were recorded
        Vector audioVec = es.getAudioClips();
        for (int i=0; i < audioVec.size(); i++) {
            //addLogMessage("adding an audio to PostData");
            addAudio((byte[]) audioVec.elementAt(i));
            //addLogMessage("FINISHED adding an audio to PostData");
        }
        
        addTags(StringifyTags(es.getTags()));
        
        // Process the activity type, which will be any of the following
        // three Strings: "Meal", "Exercise", or "Other"
        String activity_type = es.getActivityType();
        addActivityType(activity_type);
        
        if (activity_type == ActivityChooserScreen.MEAL) {
            // Process the meal descriptor, which is an array of three
            // numbers representing Carbohydrates, Fat, and Protein
            int[] meal_descriptor = es.getMealDescriptor();
            addMealDescriptor(meal_descriptor);
        }
        else if (activity_type == ActivityChooserScreen.EXERCISE) {
            int[] exercise_descriptor = es.getExerciseDescriptor();
            addExerciseDescriptor(exercise_descriptor);
        }   
    }
    
    public void addImage(byte[] img) {
 
        setHasAttachments(true);
        
        if (mySampleImage == null) {
            mySampleImage = img;
        }
        
       StringBuffer img_buf = new StringBuffer();
       Base64.encode(img,0,img.length, img_buf);
      
        myPayload += "--" + BOUNDARY +  
                getImageHeader() + 
                "\r\n" +
                img_buf.toString() + "\r\n";
               
    }
    
     public void addAudio(byte[] audio) {
       
       setHasAttachments(true);

       myPayload += "--" + BOUNDARY +
               getAudioHeader() +
               "\r\n" +
               Base64.encode(audio) + "\r\n";
    }
     
     public void addPreTags(String pre_tags) {
        myPayload += "--" + BOUNDARY +
                "Content-Disposition: form-data; name=\"pre_tags\"\r\n" +
                "Content-type: text/plain\r\n" + 
                "\r\n" +
                 pre_tags + "\r\n";
    }
     
     public void addMealDescriptor(int[] meal_descriptor) {
        
        String meal_descriptor_string = "";
        meal_descriptor_string = meal_descriptor[0] + ";" + meal_descriptor[1] + ";" +
                                 meal_descriptor[2];
       
        Logger.getLog().addLogMessage("MEAL DESC IS: " + meal_descriptor_string, Logger.MSG_INFO);
        
        myPayload += "--" + BOUNDARY +
                
                "Content-Disposition: form-data; name=\"meal_descriptor\"\r\n" +
                "Content-type: text/plain\r\n" +
                "\r\n" +
                 meal_descriptor_string + "\r\n";
    }
     
     
     public void addExerciseDescriptor(int[] exercise_descriptor) {
        
        String exercise_descriptor_string = "";
        exercise_descriptor_string = exercise_descriptor[0] + ";" + exercise_descriptor[1];
        
        
        myPayload += "--" + BOUNDARY +
                "Content-Disposition: form-data; name=\"exercise_descriptor\"\r\n" +
                "Content-type: text/plain\r\n" + 
                "\r\n" +
                 exercise_descriptor_string + "\r\n";
    }
     
     public void addActivityType(String activity_type) {
        myPayload += "--" + BOUNDARY +
                "Content-Disposition: form-data; name=\"activity_type\"\r\n" +
                "Content-type: text/plain\r\n" + 
                "\r\n" +
                 activity_type + "\r\n";
    }
     
     private String getAudioHeader() {
        
        Random rd = new Random();
        int random_int = rd.nextInt();
        
        String audio_header = "Content-Disposition: form-data; name=\"audio_file_" +
                              String.valueOf(random_int) + "\";" +
                              " filename=\"a_filename.amr\"\r\n" +
                              "Content-type: audio/x-amr\r\n" +
                              "Content-transfer-encoding: base64\r\n";
        
        return audio_header;
    }
    
    private String getImageHeader() {
        
        Random rd = new Random();
        int random_int = rd.nextInt();
        
        String image_header = "Content-Disposition: form-data; name=\"img_file_" + 
                                                               String.valueOf(random_int) + "\";" +
                                                               " filename=\"a_filename.jpg\"\r\n" +
                              "Content-type: image/jpeg\r\n" +
                              "Content-transfer-encoding: base64\r\n";
        
        return image_header;
    }
}
